package com.amado.course16SinglyNode;

import java.util.Scanner;

public class SinglyNodeTest {
	private SinglyNode fristNode = new SinglyNode("code1", "头节点");
	private Scanner scanner = new Scanner(System.in);

	SinglyNodeTest() {
		SinglyNode secondNode = new SinglyNode("code2", "节点2");
		SinglyNode threeNode = new SinglyNode("code3", "节点3");
		fristNode.setNext(secondNode);
		secondNode.setNext(threeNode);
//		fristNode.getNext()//secondNode
//		secondNode.getNext();//threeNode
	}

	public void addNode() {
		System.out.println("请输入你要插入的节点后位置");
		String afterNodeCode = scanner.next();
		SinglyNode afterNode = getNode(afterNodeCode);
		//不存在
		if(afterNode==null) {
			System.out.println("你输入的节点不存在");
			return;
		}
		System.out.println("请输入你的节点code");
		String nodeCode = scanner.next();
		//已存在
		if (getNode(nodeCode) != null) {
			System.out.println("请输入你的节点CODE已经存在");
			return;
		}
		System.out.println("请输入你的节点name");
		String nodeName = scanner.next();
		SinglyNode newNode = new SinglyNode(nodeCode,nodeName);
		
		SinglyNode nextNode = afterNode.getNext();
		afterNode.setNext(newNode);
		newNode.setNext(nextNode);
	}

	public SinglyNode getNode(String nodeCode) {
		// 判断节点是否存在
		SinglyNode tempNode = fristNode;
		while (tempNode != null) {
			if (tempNode.getCode().equals(nodeCode)) {
				return tempNode;
			}
			tempNode = tempNode.getNext();
		}
		return null;
	}

	public void print() {
		//头节点  节点2  节点3 
		SinglyNode tempNode = fristNode;
		//tempNode = 头节点
		while (tempNode != null) {
			System.out.print(tempNode.getName() + "\t");
			tempNode = tempNode.getNext();
			//tempNode = 节点2 
			//tempNode = 节点3
			//tempNode = null
		}
	}

	public static void main(String[] args) {
		SinglyNodeTest test = new SinglyNodeTest();
//		test.addNode();
		test.print();
	}
}
